home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14397 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to detect if a number has fractions
  5. Date: Sun, 14 Apr 96 11:07:25 GMT
  6. Organization: none
  7. Message-ID: <829480045snz@genesis.demon.co.uk>
  8. References: <4klb63$h4e@gate.compart.fi>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4klb63$h4e@gate.compart.fi>
  15.            joonas.kervinen@pcb.compart.fi "joonas kervinen" writes:
  16.  
  17. >Here's a small detail  that's bugging me: How do I detect in Borland C
  18. >(BCC4.5 or 5.0) whether a double value has fractions. And I mean
  19. >quickly, not using time consuming floating point functions (floor()
  20. >ceil() etc.).
  21.  
  22. These functions are probably your best method. Have you actually timed them
  23. or do you simply believe they are slow because they are functions? floor(),
  24. ceil() and modf() are all possibilities.
  25.  
  26. The question though is why you would want to do this. Floating point
  27. arithmetic is inherently inaccurate and not even guaranteed to be able
  28. to represent whole numbers precisely (although your particular implementation
  29. might provide some guarantees in that area). So even using floor() etc.
  30. you may not get any meaningful results.
  31.  
  32. >I got many replies using something like:
  33. >if (doublenum==(double)(int)doublenum)
  34. >  puts("no fractions");
  35. >
  36. >But unfortunately it does work for numbers below (about) 32000. Using
  37. >long helps a little but not all. So the question remains.
  38. >
  39. >Another addition: I'd also like to know how many faction (like 3 in
  40. >23.234) a double contains.
  41.  
  42. Very few decimal fractions can be represented precisely in binary (which is
  43. how floating point numbers are represented internally). For instance the
  44. binary representation of 1.0/10.0 is an infinite recurring expansion
  45. which the system will approximate to the precision of the floating point
  46. format used. If you try to convert that number back to decimal you will get
  47. a long expansion that is close to 0.1. To make your question meaningful
  48. you must add extra constraints e.g. convert to decimal with a limited
  49. number of decimal places or significant figures, eliminate trailing zeros
  50. (after the decimal point) and then see how many digits you have after the
  51. point.
  52.  
  53. -- 
  54. -----------------------------------------
  55. Lawrence Kirby | fred@genesis.demon.co.uk
  56. Wilts, England | 70734.126@compuserve.com
  57. -----------------------------------------
  58.